(ns com.yawmark.euler.clj.core
    (:use [clojure.contrib.lazy-seqs :only (primes)])
    (:use [clojure.contrib.math :only (expt)]))

(defn prime? [n]
  (if (> 2 n)
    false
    (not-any? #(zero? (rem n %)) (take-while #(<= (* % %) n) primes))))

(defn prime-factors-of [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
    (if (= f n) #{f} (conj (prime-factors-of (/ n f)) f))))

(defn as-int [coll]
  (reduce #(+ (* 10 %) %2) coll))

(defn as-digits [n]
  (map #(Character/getNumericValue %) (str n)))

(defn all-unique? [digits]
  (= (count (distinct digits)) (count digits)))

(defn n-pandigital?
  "Checks that a list of digits is pandigital(n)"
  [n digits]
  (and (nil? (some zero? digits))
       (nil? (some #(< n %) digits))
       (= n (count digits))
       (all-unique? digits)))

